<?php
namespace modules\game\services\method;

class FourPByTwo extends IMethod{
	
	public static $_name = __CLASS__;

	//检测牌
	public function test(Array $testPoker){
		$this->_testNum = count($testPoker);
		$this->_testPoker = $testPoker;
		if($this->_testNum !== 6) return false;
		$poker = [];
		for($i = 0; $i < $this->_testNum ; ++$i){
			$key = floor($this->_testPoker[$i] / 10);
			if(isset($poker[$key])){
				$poker[$key][] = $this->_testPoker[$i];
			}else{
				$poker[$key] = [$this->_testPoker[$i]];
			}
		}
		$flag = false;
		foreach( $poker as $val){
			if(count($val) === 4){
				$flag = true;
				break;
			}
		}
		return $flag;
	}
	/**
	 * 出牌策略
	 * @param  array $userPoker 用户当前的牌,已模块
	 * @return array 出的牌 | bool 没有此类型的牌，不出
	 */
	public function out($userPoker){//出牌
		//先选出四张
		$four = self::getInstance(FourP::$_name)->out($userPoker);
		if($four){
			$single = array_filter($userPoker,function($a){return count($a) === 1;});
			if(empty($single) || count($single) < 2) return false;
			$four = array_merge($four ,current($single));
			$four = array_merge($four ,next($single));
			sort($four);
			if($this->test($four)){
				return $four;
			}	
		}
		return false;
	}
	/**
	 * 压牌策略
	 * @param  array $userPoker     我目前的牌
	 * @param  array $opponentPoker 当前需要被压的牌
	 * @param  bool  $intense  是否尽力压牌  true是  or false 否
	 * @return array  出牌 | bool: false 压不住
	 */
	public function suppress($userPoker,$opponentPoker,$intense){
		$oppoFour = array_filter($opponentPoker,function($a){
			return count($a) === 4;
		});
		//先压四张
		$userFour = self::getInstance(FourP::$_name)->suppress($userPoker,$oppoFour,false);
		if($userFour){
			$validOne = array_filter($userPoker,function($a){return count($a) === 1;});
			if(count($validOne) >= 2 ){
				$userFour = array_merge($userFour, current($validOne));
				$userFour = array_merge($userFour, next($validOne));
				if($this->test($userFour)){
					return $userFour;
				}
			}
		}
		if($intense){//强制拆牌出牌
			return $this->forceSuppress($userPoker,$opponentPoker);
		}
		return false;
	}
}